//
//  TagFlowView.swift
//  QZWM_User_new
//
//  Created by youai on 2019/4/23.
//  Copyright © 2019年 youi. All rights reserved.
//

import Foundation
import UIKit


class TagFlowView : UIScrollView {
    
    var width : CGFloat = 0
    var height : CGFloat = 0 {
        didSet {
            self.contentSize = CGSize(width: width, height: height)
        }
    }
    
    var isDirty : Bool = false
    var list = [String]() {
        didSet{
            isDirty = true
        }
    }
    
    var textFont : UIFont = UIFont.systemFont(ofSize: 13)
    var textColor : UIColor = .black
    var horizontalPadding:CGFloat = 0
    var lineHeight:CGFloat = 0
    var horizontalMargin:CGFloat = 0
    var verticalMargin:CGFloat = 0
    var roundCorner:CGFloat = 0
    var fillColor : UIColor = .clear
    var borderColor : UIColor = .black
    var seltextColor : UIColor?
    var selfillColor : UIColor?
    var selborderColor : UIColor?
    var curView : UIView?
    
    func setProp(
        textFont : UIFont = UIFont.systemFont(ofSize: 13),
        textColor : UIColor = .black,
        horizontalPadding:CGFloat = 0,
        lineHeight:CGFloat = 0,
        horizontalMargin:CGFloat = 0,
        verticalMargin:CGFloat = 0,
        cornerRadius:CGFloat = 0,
        fillColor : UIColor = .clear,
        borderColor : UIColor = .black
        ){
        self.textFont=textFont
        self.textColor=textColor
        self.horizontalPadding=horizontalPadding
        self.lineHeight=lineHeight
        self.horizontalMargin=horizontalMargin
        self.verticalMargin=verticalMargin
        self.roundCorner=cornerRadius
        self.fillColor=fillColor
        self.borderColor=borderColor
        /*属性含义(setProp)
        textFont : UIFont  字体
        textColor : UIColor = 文字颜色
        horizontalPadding:CGFloat = 水平方向上，文字和字块边框之间的空间
        lineHeight:CGFloat  垂直方向上，单个字块的高度
        horizontalMargin:CGFloat 水平方向上，字块和字块之间的空间
        verticalMargin:CGFloat  垂直方向上，字块和字块之间的空间
        cornerRadius:CGFloat 圆角值
        fillColor : UIColor  背景色
        borderColor : UIColor 边框颜色*/
    }
    
    var tapCallback: ((String)->())? = nil
    func addTapGRToBoxes(callback:@escaping (String)->()){
//        for box in subviews {
//            let tap = UITapGestureRecognizer()
//            tap.addTarget(self, action: #selector(onBoxTap(tap:)))
//            box.addGestureRecognizer(tap)
//        }
        tapCallback = callback
    }
    
    @objc func onBoxTap(tap:UITapGestureRecognizer){
        let view = tap.view!
        let lab = view.subviews[0] as! UILabel
        if curView != nil {
            let curLab = curView!.subviews[0] as! UILabel
            curView!.backgroundColor = fillColor
            curLab.textColor = textColor
            curView!.layer.borderColor = borderColor.cgColor
        }
        if let _ = seltextColor {
            lab.textColor = seltextColor
        }
        if let _ = selfillColor {
            view.backgroundColor = selfillColor
        }
        if let _ = selborderColor {
            view.layer.borderColor = selborderColor?.cgColor
        }
        
        curView = view
        tapCallback?(list[tap.view!.tag])
    }
    
    
    func addTextBoxes(onlyTwoLine:Bool = false){
//        assert(width > 0)
//        assert(list.count > 0)
        if isDirty {
            for v in self.subviews {
                v.removeFromSuperview()
            }
        }
        
        var currentX:CGFloat = 0
        var currentY:CGFloat = 0
        var firstLine = true
        for (idx,str) in list.enumerated() {
            //先生成文字连框
            let box = generate(str)
            box.tag = idx
            
            //添加事件
            let tap = UITapGestureRecognizer()
            tap.addTarget(self, action: #selector(onBoxTap(tap:)))
            box.addGestureRecognizer(tap)
            
            if firstLine {
                //不考虑margin,不考虑换行
                addSubview(box)
                box.center = CGPoint(x: currentX, y: currentY)
                currentX += box.bounds.width
                firstLine = false
            }else{
                if currentX+horizontalMargin+box.bounds.width>width { //换行
                    if (currentY > 0) && onlyTwoLine {
                        break
                    }
                    currentY += lineHeight + verticalMargin
                    currentX = 0
                    addSubview(box)
                    box.center = CGPoint(x: currentX, y: currentY)
                    currentX += box.bounds.width
                }else{ //不换行
                    currentX += horizontalMargin
                    addSubview(box)
                    box.center = CGPoint(x: currentX, y: currentY)
                    currentX += box.bounds.width
                }
            }
        }
        height = currentY + lineHeight
//        print("store cell",height)
        isDirty = false
    }
    
    func generate(_ str:String)->UIView{
        let label = UILabel()
        label.font = textFont
        label.textColor = textColor
        label.text = str
        label.sizeToFit()
        if label.bounds.size.width > 335 {
            label.bounds.size.width = 335
        }
        let box = UIView()
        box.layer.anchorPoint = CGPoint.zero
        box.bounds = CGRect(origin: CGPoint.zero, size: CGSize(width: label.bounds.width+horizontalPadding*2, height: lineHeight))
        box.addSubview(label)
        label.numberOfLines = 1
        label.layer.anchorPoint = CGPoint.zero
        label.center = CGPoint(x: horizontalPadding, y: (lineHeight-label.bounds.height)/2.0)
        box.backgroundColor = fillColor
        if borderColor != UIColor.clear {
            box.layer.borderWidth = 1
            box.layer.borderColor = borderColor.cgColor
        }
        if roundCorner != 0 {
            box.cornerRadius(roundCorner)
        }
        return box
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        if !isDirty {
            return
        }
        width = bounds.width
        if width <= 0 || list.count <= 0 {
            return
        }
        addTextBoxes()
    }
}

